/************************************************************************************
**  
* @copyright (c) 2015-, Technology Co., LTD. All Right Reserved.
*
*************************************************************************************/
/**
* @file     duye_cap_linklayer_service.h
* @version     
* @brief      
* @author   
* @date     2013-01-08
* @note 
*
*  1. 2018-01-08 created this file
* 
*/
#pragma once

#include <string>
#include <map>

#include <duye_type.h>

#include <duye_cap_packet.h>
#include <duye_cap_service_base.h>
#include <duye_cap.h>

namespace duye {

/**
 * @brief link layer listener interface
 */
class LinkLayerListener : public DataServiceListener {
public:
	virtual ~LinkLayerListener() {}
    virtual int32 onLinkLayerPacket(const PacketReference& pktRef) = 0;
};

typedef std::map<LinkLayerListener*, FilterRule*> LinkLayerListenerMap;

class LinkLayerService : public CapDataService, public Singleton<LinkLayerService> {
public:
	LinkLayerService();
	virtual ~LinkLayerService();

	// implementation WdpiService interface 
	virtual bool start();
    // implementation WdpiService interface
	virtual bool stop();
	// implementation WdpiService interface
	virtual bool run();
    // implementation WdpiService interface
    virtual bool addListener(DataServiceListener* listener, const FilterRule* filterRule = NULL);
    // implementation WdpiService interface
    virtual bool delListener(DataServiceListener* listener); 

	void setNetcard(const std::string& netcard);
	void setCapfile(const std::string& capfile);
	
private:
	// packet handle function
	void packetHandle(const PcapPkthdr* pcapPktHeader, const uint8* pkt);

	// pcap callback function
	static void pcapCallback(void* user, const PcapPkthdr* pcapPktHeader, const uint8* pkt);

private:
	std::string m_netcard;
	std::string m_capFile;
    LinkLayerListenerMap m_listenerMap;
};

}
